package utils
{
	import flash.display.CapsStyle;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.JointStyle;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	public class DisplayUtil
	{
		public function DisplayUtil()
		{
		}
		
		public static const GrayFilter:ColorMatrixFilter = new ColorMatrixFilter([  
			1,0,0,0,0,  
			1,0,0,0,0,  
			1,0,0,0,0,  
			0,0,0,1,0]);
		
		public static function toGray(source:DisplayObject):void
		{
			source.filters = [GrayFilter];
		}
		
		public static function removeChildren(container:DisplayObjectContainer):void
		{
			while (container.numChildren > 0)
			{
				container.removeChildAt(0);
			}
		}
		
		public static function stopAll(mc:DisplayObjectContainer):void
		{
			if (mc is MovieClip)
			{
				(mc as MovieClip).stop();
			}
			var child:DisplayObjectContainer;
			for (var i:int = 0; i < mc.numChildren; i++) 
			{
				child = mc.getChildAt(i) as DisplayObjectContainer;
				if (child != null) 
				{
					stopAll(child);
				}
			}
		}
		
		public static function startAll(mc:DisplayObjectContainer):void
		{
			if(mc is MovieClip)
			{
				(mc as MovieClip).play();
			}
			var child:DisplayObjectContainer;
			for (var i:int = 0; i < mc.numChildren; i++) 
			{
				child = mc.getChildAt(i) as DisplayObjectContainer;
				if (child != null) 
				{
					startAll(child);
				}
			}
		}
		
		public static function nextFrameAll(container:DisplayObjectContainer):void
		{
			var mc:MovieClip = container as MovieClip;
			if (mc != null)
			{
				mc.gotoAndStop(mc.currentFrame % mc.totalFrames + 1);
			}
			var child:DisplayObjectContainer;
			for (var i:int = 0; i < mc.numChildren; i++) 
			{
				child = mc.getChildAt(i) as DisplayObjectContainer;
				if (child != null) 
				{
					nextFrameAll(child);
				}
			}
		}
		
		public static function disbleMouse(mc:MovieClip):void {
			mc.mouseEnabled = false;
			mc.mouseChildren = false;
		}
		
		public static function fitTo(v:DisplayObject, w:int, h:int):void {
			var scaleX:Number = w / v.width;
			var scaleY:Number = h / v.height;
			v.scaleX = v.scaleY = Math.min(scaleX, scaleY);
		}
		
		public static function drwaCrood(v:Sprite):void {
			var g:Graphics = v.graphics;
			g.lineStyle(1, 0x00FF00);
			g.moveTo(-100, 0);
			g.lineTo(100, 0);
			g.lineStyle(1, 0x0000BB);
			g.moveTo(0, -100);
			g.lineTo(0, 100);
		}
		
		public static function alignCenterTo(v:DisplayObject, x:int , y:int):void {
			var rect:Rectangle = v.getBounds(v);			
			v.x = x -v.width/2 - rect.x;
			v.y = y -v.height/2 - rect.y;
		}
	}
}